package com.ruoyi.training.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.training.domain.ExamRecord;
import com.ruoyi.training.domain.TrainPlan;
import com.ruoyi.training.mapper.ExamRecordMapper;
import com.ruoyi.training.mapper.TrainPlanMapper;
import com.ruoyi.training.service.IExamRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 培训记录Service业务层处理
 *
 * @author wng
 * @date 2024-08-04
 */
@Service
public class ExamRecordServiceImpl implements IExamRecordService {
    @Resource
    private ExamRecordMapper examRecordMapper;
    @Resource
    private TrainPlanMapper trainPlanMapper;

    /**
     * 查询培训记录
     *
     * @param id 培训记录主键
     * @return 培训记录
     */
    @Override
    public ExamRecord selectExamRecordById(Long id) {
        return examRecordMapper.selectExamRecordById(id);
    }

    /**
     * 查询培训记录列表
     *
     * @param examRecord 培训记录
     * @return 培训记录
     */
    @Override
    public List<ExamRecord> selectExamRecordList(ExamRecord examRecord) {
        if (examRecord.getParams().containsKey("examTimeStart") && examRecord.getParams().containsKey("examTimeEnd")) {
            if (examRecord.getParams().get("examTimeStart") != null && !"".equals(examRecord.getParams().get("examTimeStart"))
                    && examRecord.getParams().get("examTimeEnd") != null && !"".equals(examRecord.getParams().get("examTimeEnd"))) {
                examRecord.getParams().put("examTimeStart", examRecord.getParams().get("examTimeStart") + " 00:00:00");
                examRecord.getParams().put("examTimeEnd", examRecord.getParams().get("examTimeEnd") + " 23:59:59");
            }

        }
        if (examRecord.getParams().containsKey("signTimeStart") && examRecord.getParams().containsKey("signTimeEnd")) {
            if (examRecord.getParams().get("signTimeStart") != null && !"".equals(examRecord.getParams().get("signTimeStart"))
                    && examRecord.getParams().get("signTimeEnd") != null && !"".equals(examRecord.getParams().get("signTimeEnd"))) {
                examRecord.getParams().put("signTimeStart", examRecord.getParams().get("signTimeStart") + " 00:00:00");
                examRecord.getParams().put("signTimeEnd", examRecord.getParams().get("signTimeEnd") + " 23:59:59");
            }

        }
        List<ExamRecord> examRecords = examRecordMapper.selectExamRecordList(examRecord);
        examRecords.forEach(item -> {
            TrainPlan trainPlan = trainPlanMapper.selectTrainPlanById(item.getPlanId());
            item.getParams().put("planTitle", trainPlan.getTitle());
            item.setPlanTitle(trainPlan.getTitle());

            //人员姓名
            item.setUserName(examRecordMapper.selectSysUserNameById(item.getUserId()));
        });

        return examRecords;
    }

    /**
     * 新增培训记录
     *
     * @param examRecord 培训记录
     * @return 结果
     */
    @Override
    public int insertExamRecord(ExamRecord examRecord) {
        examRecord.setCreateTime(DateUtils.getNowDate());
        return examRecordMapper.insertExamRecord(examRecord);
    }

    /**
     * 报名
     */
    @Override
    public int insertExamRecord_user(ExamRecord examRecord) {
        examRecord.setCreateTime(DateUtils.getNowDate());
        TrainPlan trainPlan = trainPlanMapper.selectTrainPlanById(examRecord.getPlanId());

        List<ExamRecord> examRecords = examRecordMapper.selectExamRecordList(new ExamRecord() {{
            setDeptId(trainPlan.getDeptId());
            setPlanId(trainPlan.getId());
            setUserId(examRecord.getUserId());
        }});
        if (!examRecords.isEmpty()) {
            throw new ServiceException("已经报名过该培训计划");
        }

        examRecord.setSignTime(new Date());
        examRecord.setExamTime(trainPlan.getExamTime());

        //当前报名人的所属部门
        examRecord.setDeptId(examRecordMapper.selectSysUserDeptIdById(examRecord.getUserId()));

        return examRecordMapper.insertExamRecord(examRecord);
    }

    /**
     * 修改培训记录
     *
     * @param examRecord 培训记录
     * @return 结果
     */
    @Override
    public int updateExamRecord(ExamRecord examRecord) {
        examRecord.setUpdateTime(DateUtils.getNowDate());
        return examRecordMapper.updateExamRecord(examRecord);
    }

    /**
     * 批量删除培训记录
     *
     * @param ids 需要删除的培训记录主键
     * @return 结果
     */
    @Override
    public int deleteExamRecordByIds(Long[] ids) {
        return examRecordMapper.deleteExamRecordByIds(ids);
    }

    /**
     * 删除培训记录信息
     *
     * @param id 培训记录主键
     * @return 结果
     */
    @Override
    public int deleteExamRecordById(Long id) {
        return examRecordMapper.deleteExamRecordById(id);
    }
}
